package com.example;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author loubobooo
 * @Description #145. 二叉树的后序遍历
 * @Date 2022/3/2
 */
public class BinaryTreePostorderTraversal {

    class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    private List<Integer> list = new LinkedList<>();

    /**
     * 方法描述:给你一棵二叉树的根节点 root ，返回其节点值的 后序遍历 。
     *
     * 示例 1：
     * 输入：root = [1,null,2,3]
     * 输出：[3,2,1]
     * @param: [root]
     * @return: java.util.List<java.lang.Integer>
     * @author: loubobooo
     * @date: 2022/3/2
     */
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root == null){
            return list;
        }
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        list.add(root.val);
        return list;
    }

}
